package com.shangcheng.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsHomeNewProductMapper;
import com.shangcheng.model.domain.SmsHomeBrand;
import com.shangcheng.model.domain.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/12 22:04
 */
@DubboService
public class SmsHomeNewProductApiImpl implements SmsHomeNewProductApi{
    @Autowired
    private SmsHomeNewProductMapper smsHomeNewProductMapper;
    /**
     * 批量添加首页新品
     * @param homeNewProductList 新品集合
     */
    @Override
    public void createNewPro(List<SmsHomeNewProduct> homeNewProductList) {
        for (SmsHomeNewProduct smsHomeNewProduct : homeNewProductList) {
            smsHomeNewProductMapper.insert(smsHomeNewProduct);
        }
    }

    /**
     * 批量删除首页新品
     * @param ids 新品ids
     */
    @Override
    public void deleteNewPro(Integer[] ids) {
            smsHomeNewProductMapper.deleteBatchIds(Arrays.asList(ids.clone()));
    }

    /**
     * 分页查询首页新品
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @Override
    public IPage<SmsHomeNewProduct> newProList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        IPage<SmsHomeNewProduct> page= new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(productName),SmsHomeNewProduct::getProductName,productName);
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus),SmsHomeNewProduct::getRecommendStatus,recommendStatus);
        return smsHomeNewProductMapper.selectPage(page,wrapper);
    }

    /**
     * 批量修改新品推荐状态
     * @param ids
     * @param recommendStatus
     */
    @Override
    public void updateRecommendStatus(Integer[] ids, Integer recommendStatus) {
        for (Integer id : ids) {
            SmsHomeNewProduct product =new SmsHomeNewProduct();
            product.setId(id.longValue());
            product.setRecommendStatus(recommendStatus);
            smsHomeNewProductMapper.updateById(product);
        }
    }

    /**
     * 修改新品排序
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Integer id, Integer sort) {
        SmsHomeNewProduct product=new SmsHomeNewProduct();
        product.setId(id.longValue());
        product.setSort(sort);
        LambdaUpdateWrapper<SmsHomeNewProduct> wrapper= new LambdaUpdateWrapper<>();
        wrapper.eq(SmsHomeNewProduct::getId,product.getId());
        wrapper.set(!ObjectUtils.isEmpty(sort),SmsHomeNewProduct::getSort,product.getSort());
        smsHomeNewProductMapper.update(product,wrapper);
    }

    /**
     * 根据产品名称查询新新品
     * @param homeNewProductList
     * @return
     */
    @Override
    public List<SmsHomeNewProduct> findByName(List<SmsHomeNewProduct> homeNewProductList) {
        List<String> productNames = CollUtil.getFieldValues(homeNewProductList, "productName", String.class);
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper= new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeNewProduct::getProductName,productNames);
        return smsHomeNewProductMapper.selectList(wrapper);
    }
}
